var requests = require('../../requests/request.js');
var utils = require('../../utils/util.js');
// mytest.js
Page({
  data: {
    isTest:false,         //是否有参加过测试
    session3rd:'',       //登录态
    loadingmore:true,   //加载更多
    start:0,            //从第0条开始请求
    pageSize:5,         //每次请求数据的条数
    listData:[]       //列表数据
  },
  onLoad: function (options) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 10000,
    })
    const that = this;
    requsetMyTestList.call(that);
  },
  //重新测试
  retest: function (event) {
    const id = event.target.dataset.id;
    wx.navigateTo({
      url: `../starttest/starttest?id=${id}`
    })
  },
  //查看结果
  viewResult: function(event){
    const resultId = event.target.dataset.resultid;  //测试结果id
    wx.navigateTo({
      url: `../viewresult/viewresult?resultId=${resultId}`
    })
  },
  //上拉加载
  onReachBottom: function () {    
    console.log('loading...')
    wx.showToast({
      title: '加载中...',
      icon: 'loading',
      duration: 1500,
    })
    requsetMyTestList.call(this);
  },
})

//请求列表数据
function requsetMyTestList() {
  const that = this;
  var myTestData = this.data.listData;
  var start = this.data.start;
  var pageSize = this.data.pageSize;
  wx.getStorage({
    key: '3rd_session',
    success: function (res) {
      // console.log(res)
      const session = res.data;
      that.setData({ session3rd: session });
      requests.requestMyTest(           //请求列表数据
        {
          session: session,
          select_time: start,
          limit: pageSize
        }, (res) => {
          // console.log(res)
          setTimeout(function () {
            wx.hideToast()
          }, 500)
          if (res.state == 2) {          //没做过测试
            that.setData({ isTest: true });
          } else if (res.state == 1 ){   //列表数据
            that.setData({
              listData: myTestData.concat(res.recode_list),
              start: res.select_time
            });
          } else if (res.state == 3 ){    //加载完毕   没有更多
            that.setData({ loadingmore: false });
          }
        }, () => {
          //请求失败
        }, () => {
          //请求完成
        }
      )
    },
  })
}